"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DbClient = exports.Transaction = exports.DbClientNotFoundError = void 0;
class DbClientNotFoundError extends Error {
    constructor(message = 'No result!') {
        super(message);
        this.name = 'DbClientNotFoundError';
    }
}
exports.DbClientNotFoundError = DbClientNotFoundError;
class Transaction {
}
exports.Transaction = Transaction;
class DbClient {
    constructor({ host, port, database, user, password }) {
        this.host = host;
        this.port = port;
        this.database = database;
        this.user = user;
        this.password = password;
    }
    columnProcess(s) {
        return s;
    }
    returnJsonProcess(result, tableConfig) {
        if (tableConfig.jsonColumn) {
            tableConfig.jsonColumn.forEach(it => {
                if (result[it]) {
                    try {
                        result[it] = JSON.parse(result[it]);
                    }
                    catch (e) {
                        console.error(`Failed to parse JSON for column ${it}:`, e);
                    }
                }
            });
        }
    }
    escapeValue(val) {
        if (val === null || val === undefined) {
            return 'NULL';
        }
        if (typeof val === "string") {
            return `'${val.replace(/'/g, "''")}'`;
        }
        if (typeof val === "boolean") {
            return val ? 'TRUE' : 'FALSE';
        }
        if (typeof val === "number") {
            return val.toString();
        }
        if (val instanceof Date) {
            return `'${val.toISOString()}'`;
        }
        return `'${JSON.stringify(val).replace(/'/g, "''")}'`;
    }
}
exports.DbClient = DbClient;
